package com.jc.service.producttype;

import com.jc.cds.model.CustomProductType;
import com.jc.cds.model.CustomRole;
import com.jc.common.util.CheckNullUtils;
import com.jc.ds.base.service.BaseServiceImpl;
import com.jc.ds.mapper.BaseCustomMapper;
import com.jc.ds.model.ProductType;
import com.jc.ds.model.Role;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductTypeServiceImpl extends BaseServiceImpl<ProductType, CustomProductType, Long> implements ProductTypeService {

    @Override
    public BaseCustomMapper<CustomProductType, Long> getCustomMapper() {
        return null;
    }

    /**
     * 保存商城分类时同时新增OMS的分类(编辑)
     */
    @Override
    @Transactional
    public Map<String, Object> addOMSProductType(Map<String, Object> params) {

        Map<String, Object> result = new HashMap<>();

        if (CheckNullUtils.isNull(String.valueOf(params.get("addCategoryName")))) {
            result.put("data", "OMS缺少必填参数分类名字");
            result.put("msg", "OMS缺少必填参数分类名字");
            result.put("code", 6666);
            return result;
        }
        if (CheckNullUtils.isNull(String.valueOf(params.get("idEdit")))) {
            result.put("data", "OMS缺少必填参数是否编辑模式");
            result.put("msg", "OMS缺少必填参数是否编辑模式");
            result.put("code", 6666);
            return result;
        }
        if (CheckNullUtils.isNull(String.valueOf(params.get("beforeClick")))) {
            result.put("data", "OMS缺少必填参数beforeClick");
            result.put("msg", "OMS缺少必填参数beforeClick");
            result.put("code", 6666);
            return result;
        }
        if (String.valueOf(params.get("idEdit")).equals("No")) {
            List<ProductType> addCategoryName = super.queryAll(new ProductType() {{
                setTypeName(String.valueOf(params.get("addCategoryName")));
            }});
            if (addCategoryName.size() > 0) {
                result.put("data", "OMS分类已经存在，无法新增！");
                result.put("msg", "OMS分类已经存在，无法新增！");
                result.put("code", 6666);
                return result;
            }
            super.add(new ProductType() {{
                setTypeName(String.valueOf(params.get("addCategoryName")));
                setCreateTime(new Date());
            }}, false);
        } else {
            List<ProductType> addCategoryName = super.queryAll(new ProductType() {{
                setTypeName(String.valueOf(params.get("beforeClick")));
            }});
            if (addCategoryName.size() != 1) {
                result.put("data", "OMS分类不存在或者存在多个，无法编辑！");
                result.put("msg", "OMS分类不存在或者存在多个，无法编辑！");
                result.put("code", 6666);
                return result;
            }
            super.modify(new ProductType() {{
                setId(addCategoryName.get(0).getId());
                setTypeName(String.valueOf(params.get("categoryName")));
                setCreateTime(new Date());
            }}, false);
        }
        result.put("data", "保存成功！");
        result.put("msg", "保存成功");
        result.put("code", 10000);
        return result;
    }

    /**
     * 删除商城分类时同时 删除OMS的分类
     */
    @Override
    @Transactional
    public Map<String, Object> delOMSProductType(Map<String, Object> params) {

        Map<String, Object> result = new HashMap<>();

        if (CheckNullUtils.isNull(String.valueOf(params.get("categoryName")))) {
            result.put("data", "OMS缺少必填参数categoryName");
            result.put("msg", "OMS缺少必填参数categoryName");
            result.put("code", 6666);
            return result;
        }

        List<ProductType> categoryName = super.queryAll(new ProductType() {{
            setTypeName(String.valueOf(params.get("categoryName")));
        }});
        if (categoryName.size() != 1) {
            result.put("data", "OMS分类不存在或者存在多个，无法删除！");
            result.put("msg", "OMS分类不存在或者存在多个，无法删除！");
            result.put("code", 6666);
            return result;
        }
        super.remove(categoryName.get(0).getId());
        result.put("data", "删除成功！");
        result.put("msg", "删除成功");
        result.put("code", 10000);
        return result;

    }
}
